package cn.itcast.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.itcast.dao.ReportDao;

import cn.itcast.entity.Result;
import cn.itcast.pojo.Business;
import cn.itcast.pojo.HotSetmeals;
import cn.itcast.pojo.Report;

import cn.itcast.service.ReportService;
import cn.itcast.utils.DateUtils;
import cn.itcast.vo.FindMemberCountVo;
import cn.itcast.vo.FindSetmealVo;

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDao;


    /**
     * 每月收入统计
     *
     * @return
     */
    @Override
    public Report selectIncome() {

        Report report = new Report();
        //收入
        List<Double> incomesList = new ArrayList<>();
        try {
            //近12个月份
            List<String> mouths = this.getMouth(report);
            mouths.stream().forEach(mouth -> {
                Double money = reportDao.selectIncome(mouth);
                incomesList.add(money);
            });
            report.setIncomes(incomesList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return report;
    }

    /**
     * 预约/支付统计
     *
     * @return
     */
    @Override
    public Report selectOrder() {
        Report report = new Report();
        //收入
        List<Double> incomesList = new ArrayList<>();
        //预约
        ArrayList<Integer> reserveList = new ArrayList<>();
        try {
            //近12个月份
            List<String> mouths = this.getMouth(report);
            mouths.stream().forEach(mouth -> {
                Double money = reportDao.selectIncome(mouth);
                Integer order = reportDao.selectOrder(mouth);
                incomesList.add(money);
                reserveList.add(order);
            });
            report.setIncomes(incomesList);
            report.setReserve(reserveList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return report;
    }

    @Override
    public FindMemberCountVo findMemberCount() {
        Report report=new Report();
        try {
            List<String> mouths = this.getMouth(report);
            List<Integer> memberCount=new ArrayList();
            for (String mouth : mouths) {
                Integer count = reportDao.selectCount(mouth+"-28");
                memberCount.add(count);
            }
            FindMemberCountVo vo=new FindMemberCountVo();
            vo.setMonths(mouths);
            vo.setMemberCount(memberCount);
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<FindSetmealVo> findSetmeals() {
        List<FindSetmealVo> list =reportDao.findsetmeals();

        return list;
    }
    /**
     * 运营数据统计
     *
     * @param business
     * @return
     */
    @Override
    public Business selectBusiness(Business business) {
        //获取日期
        Date date = DateUtils.getToday();
        try {
            //获取本周一
            String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday(DateUtils.getToday()));
            //获取本周日
            String sundayOfThisWeek = DateUtils.parseDate2String(DateUtils.getSundayOfThisWeek());
            //获取本月一号
            String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
            //保存今日日期
            String today = DateUtils.parseDate2String(date);
            business.setReportDate(today);
            //保存今日新增会员数
            Integer todayNewMember = reportDao.selectTodayNewMember(today);
            business.setTodayNewMember(todayNewMember);
            //保存总会员数
            Integer totalMember = reportDao.selectTotalMember();
            business.setTotalMember(totalMember);
            // 保存本周新增会员数
            Integer thisWeekNewMember = reportDao.selectThisWeekAndMonthNewMember(thisWeekMonday, sundayOfThisWeek);
            business.setThisWeekNewMember(thisWeekNewMember);
            //本月新增会员数
            Integer thisMonthNewMember = reportDao.selectThisWeekAndMonthNewMember(firstDay4ThisMonth, today);
            business.setThisMonthNewMember(thisMonthNewMember);
            //今日预约数
            Integer todayOrderNumber = reportDao.selectTodayOrderNumber(today);
            business.setTodayOrderNumber(todayOrderNumber);
            //今日到诊数
            Integer TodayVisitsNumber = reportDao.selectTodayVisitsNumber(today);
            business.setTodayVisitsNumber(TodayVisitsNumber);
            //本周预约数
            Integer thisWeekOrderNumber = reportDao.selectThisWeekAndMonthOrderNumber(thisWeekMonday, sundayOfThisWeek);
            business.setThisWeekOrderNumber(thisWeekOrderNumber);
            //本月预约数
            Integer thisMonthOrderNumber = reportDao.selectThisWeekAndMonthOrderNumber(firstDay4ThisMonth, today);
            business.setThisMonthOrderNumber(thisMonthOrderNumber);
            //本周到诊数
            Integer thisWeekVisitsNumber = reportDao.selectThisWeekAndMonthVisitsNumber(thisWeekMonday, sundayOfThisWeek);
            business.setThisWeekVisitsNumber(thisWeekVisitsNumber);
            //本月到诊数
            Integer thisMonthVisitsNumber = reportDao.selectThisWeekAndMonthVisitsNumber(firstDay4ThisMonth, today);
            business.setThisMonthVisitsNumber(thisMonthVisitsNumber);
            //设置热门套餐
            List<HotSetmeals> setmeatCounts = reportDao.selectSelmeat();
            //总预约数计数器
            double count = 0;
            for (HotSetmeals hotSetmeals : setmeatCounts) {
                count += hotSetmeals.getSetmeal_count();
            }
            for (int i = 0; i < setmeatCounts.size(); i++) {
                double result = setmeatCounts.get(i).getSetmeal_count() / count;
                setmeatCounts.get(i).setProportion(result);
            }
            System.out.println(setmeatCounts);
            business.setHotSetmeal(setmeatCounts);
            return business;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取近12个月工具类
     *
     * @param report
     * @return
     * @throws Exception
     */
    private List<String> getMouth(Report report) throws Exception {
        //今天日期
        String maxTime = DateUtil.today();
        //今天月份
        int minMonth = DateUtil.month(new Date());
        String minTime = null;
        if (minMonth > 10) {
            minTime = DateUtil.year(new Date()) + "-" + 1;
        } else {
            minTime = (DateUtil.year(new Date()) - 1) + "-" + (minMonth + 2);
        }
        //工具类算出中间月份 yyyy-MM
        List<String> months = DateUtils.getMonthBetween(minTime, maxTime, "yyyy-MM");
        report.setMonths(months);
        return months;
    }
}

